"use strict";

(function($) {
  Drupal.behaviors.timeago = {
    attach: function(context, settings) {
      $.timeago.settings.strings.seconds = 'now';
      $.timeago.settings.strings.suffixAgo = '';
      $('abbr.timeago', context).once('timeago', function() {
        $(this).timeago();
      })
    }
  }
})(jQuery);

jQuery(document).ready(function($) {
  if($('.original-link-content').length) {
    $('.original-link-content').append('loading original link...');

    $.post('/reader', {
      url: Drupal.settings.board_feed.link_crabber.og_data.url,
      crabii_app_token: Drupal.settings.crabii_app.crabii_app_token
    }, function(data) {
      var output = '';

      if(data.error) {
        output = data.error;
      }
      else {
        output = '<h2 class="original-link-title">' + data.title + '</h2>';
        output += '<div class="original-link-body">' + data.body + '</div>';
      }

      $('.original-link-content').empty();
      $('.original-link-content').append(output);
      $('.original-link-content img').error(function() {
        var parsed_url = Drupal.settings.board_feed.parsed_url
        var original_host = parsed_url.scheme + '://' + parsed_url.host;
        var img_src = original_host + $(this).attr('src');
        $(this).attr('src', img_src);
      });
    });
  }
});
